package com.dz.service.impl;

import com.dz.pojo.Information;
import com.dz.mapper.InformationMapper;
import com.dz.service.InformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dz.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dz
 * @since 2021-06-06
 */
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private RedisUtil redisUtil;
    //根据id查询用户
    public Information getInfo(Integer id){
        String key="info_"+id;
        boolean b = redisUtil.hasKey(key);
        //如果redis中存在
        if (b){
            return (Information) redisUtil.get(key);
        }
        else{
            Information info = informationMapper.selectById(id);
            //将数据写入缓存
            redisUtil.set("info_"+id,info);
            return info;
        }
    }
    //删除用户
    public int deleteById(Integer id){
        int res = informationMapper.deleteById(id);
        String key="info_"+id;
        if (res!=0){
            if (redisUtil.hasKey(key)){
                redisUtil.del(key);
            }
        }
        return res;
    }
    //修改用户
    public int update(Information information){
        int res = informationMapper.updateById(information);
        String key="info_"+information.getId();
        if (res!=0){
            if (redisUtil.hasKey(key))
                redisUtil.del(key);
            //更新
            redisUtil.set("info_"+information.getId(),information);
        }
        return res;
    }
}
